const app = getApp()
Page({
  data: {
    loading: true,
    islogin:false,
    uid:0,
    show_follow:false,
    showbar:false,
    login_uid:0,
    mp_uid:0,
    mp_mid:0,
    cate_id: 0,
    mp:{},
    page: 0,
    info: [],
    shop_page:0,
    shop: [],
    active:1
  },
  //生命周期函数--监听页面加载
  onLoad: function (options) {
    app.getParam(options,(param)=>{
      if(!app.util().isNull(param.uid)){
        this.setData({
          mp_uid:parseInt(options.uid),
        });
      }else{
        this.setData({
          mp_mid:parseInt(options.mid),
        });
      }
      if(!app.util().isNull(app.globalData.loginuser)){
        this.setData({
          islogin:true,
          uid:app.globalData.loginuser.uid,
        });
      }
      this.infoMp();
      this.infoApi();
    })
    this.setData({
      startBarHeight: app.globalData.startBarHeight,
      navgationHeight: app.globalData.navgationHeight,
    });
  },
  // 下拉刷新
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    setTimeout(function () {
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
    }, 1500);
  },
  //上拉加载
  onReachBottom: function () {
    this.setData({
      loading: true,
    });
    if(this.data.active == 2){
      this.getStore();
    }else{
      this.infoApi();
    }
  },
  //获取城市号信息
  infoMp: function () {
    app.api().Get('api/v1/ais/info/mp',{uid:this.data.mp_uid,mid:this.data.mp_mid},(result)=>{
      this.setData({
        mp: result.data,
      })
    })
  },
  //点击新闻
  infoApi: function () {
    if (this.data.loading) {
      var param = {
        page: this.data.page + 1,
        state:this.data.active,
        uid:this.data.mp_uid,
        mid:this.data.mp_mid,
      }
      app.api().Get('api/v1/ais/info/me',param,(result)=>{
        if (result.code == 200) {
          var info = this.data.info;
          for (let i in result.data) {
            info.push(result.data[i]);
          }
          this.setData({
            info: info,
          });
          this.data.page = param.page
        }
        this.setData({
          loading: false,
        });
      })
    }
  },
  //点击请求数据
  getStore: function () {
    if (this.data.loading && this.data.mp.is_store) {
      var param = {
        store_id: this.data.mp.store.id,
        page: this.data.shop_page + 1,
        types:0,
      }
      app.api().Get('api/v1/ais/shop/goods', param, (result) => {
        if (result.code == 200) {
          var shop = this.data.shop;
          for (let i in result.data) {
            shop.push(result.data[i]);
          }
          this.setData({
            shop: shop,
            shop_page: param.page,
          });
        }
        this.setData({
          loading: false,
        });
      })
    }
  },
  //点赞
  onLike:function(e){
    let key = e.currentTarget.dataset.key,id = e.currentTarget.dataset.id;
    var info = this.data.info;
    if (info[key].is_like == 0){
      app.api().Post("api/v1/ais/infosend/like",{id:id},(rel) => {
        info[key].is_like = 1;
        info[key].like    = info[key].like+1;
        this.setData({
          info:info,
          onLike:true
        })
        setTimeout(()=>{
          this.setData({
            onLike:false
          })
        },600)
      })
    }
  },
  //关注与取消
  onFollow:function (e) {
    var key = e.currentTarget.dataset.key;
    var info = this.data.info;
    if (info[key].is_follow == 0){
      app.api().Post("api/v1/ais/infosend/follow",{uid:info[key].user_id,state:1},(rel) => {
        info[key].is_follow = 1;
        this.setData({
          info:info,
        })
      })
    }
  },
  //去发信息
  send:function () {
    wx.navigateTo({
      url: 'send',
    })
  },
  //返回上一页
  onClickLeft:function(){
    wx.navigateBack({
      delta: 0,
    })
  },
  //分享按钮
  onShareAppMessage: function (e) {
    return {
      title:this.data.mp.title,
      path: '/packageInfo/pages/info/me?uid=' + this.data.mp_uid+'&mid='+this.data.mp_mid
    }
  },
  //点击加载更多
  onChange: function (event) {
    var active = event.detail.index;
    this.setData({
      active: active,
    });
    if(active == 2){
      if(app.util().isNull(this.data.shop)){
        this.setData({
          loading: true,
        });
        this.getStore();
      }
    }else{
      this.setData({
        loading: true,
        page: 0,
        info: [],
      });
      this.infoApi();
    }
  },
  //拨打电话
  callPhone: function (e) {
    app.callPhone(e);
  },
  // //滚动
  // onPageScroll(res) {
  //   let scrollTop = res.scrollTop;
  //   this.setData({
  //     isnav: 250 <= scrollTop ? false : true
  //   })
  // },
  //滚动
  onPageScroll(e){
    const query = wx.createSelectorQuery()
    query.select('.profile-header').boundingClientRect()
    query.selectViewport().scrollOffset()
    query.exec((e) => {
      this.setData({
        showbar: 0 >= e[0].bottom-(app.globalData.startBarHeight+app.globalData.navgationHeight) ?true :false
      })
    })
  }
})